/**
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package xyz.geminiwen.skinsprite.app;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import xyz.geminiwen.skinsprite.resource.CustomResourceManager;

/**
 * Created by geminiwen on 16/6/15.
 */
public class SkinnableComponentLayoutScatter {

    private static volatile SkinnableComponentLayoutScatter instance;
    private LayoutScatter proxy;

    private SkinnableComponentLayoutScatter(Context context) {
        updateProxy(context);
    }

    public static SkinnableComponentLayoutScatter getInstance(Context context) {
        if (instance == null) {
            synchronized (SkinnableComponentLayoutScatter.class) {
                if (instance == null) {
                    instance = new SkinnableComponentLayoutScatter(context);
                }
            }
        }
        return instance;
    }

    public Component parse(int xmlId, ComponentContainer root, boolean attachToRoot) {
        return proxy.parse(xmlId, root, attachToRoot);
    }

    void updateProxy(Context context) {
        proxy = LayoutScatter.getInstance(context).clone(context, new CustomResourceManager(context.getResourceManager()));
    }

}
